package com.aimosheng.main.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.aimosheng.common.activity.AbsActivity;
import com.aimosheng.common.event.CoinChangeEvent;
import com.aimosheng.common.http.HttpCallback;
import com.aimosheng.common.utils.DialogUitl;
import com.aimosheng.common.utils.ToastUtil;
import com.aimosheng.main.R;
import com.aimosheng.main.adapter.TaskAdapter;
import com.aimosheng.main.bean.TaskItemBean;
import com.aimosheng.main.http.MainHttpUtil;
import com.aimosheng.main.http.TaskHttpUtil;
import com.mob.adsdk.AdSdk;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cxf on 2019/4/9.
 */

public class TaskActivity extends AbsActivity implements TaskAdapter.ActionListener {


    private static final String TAG = "TaskRewardVideo";
    private RecyclerView mTaskRecyclerView;
    private TaskAdapter mTaskAdapter;
    private Dialog loadingDialog;
    private boolean isReward;
    private long startTime;
    private boolean hasLookRewardVideoCount = true;


    public static void forward(Context context) {
        Intent intent = new Intent(context, TaskActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_task;
    }


    @Override
    protected void main() {
        setTitle("每日任务");
        mTaskRecyclerView = findViewById(R.id.rv_task);
        mTaskRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mTaskAdapter = new TaskAdapter(mContext);
        mTaskAdapter.setActionListener(this);
        List<TaskItemBean> taskItemBeans = new ArrayList<>();
        TaskItemBean taskItemBean = new TaskItemBean("每日签到","2聊米","去签到",false);
        TaskItemBean taskItemBean0 = new TaskItemBean("任务赚","最高2000/每天","去完成",false);
        TaskItemBean taskItemBean1 = new TaskItemBean("发布作品","2聊米/条","去发布",false);
        TaskItemBean taskItemBean2 = new TaskItemBean("发布动态","2聊米/条","去发布",false);
        TaskItemBean taskItemBean3 = new TaskItemBean("观看视频20分钟","20聊米","去观看",false);
        TaskItemBean taskItemBean4 = new TaskItemBean("激励视频任务","最高20聊米","去完成",false);
        TaskItemBean taskItemBean5 = new TaskItemBean("转发视频","2聊米","去分享",false);
        TaskItemBean taskItemBean6 = new TaskItemBean("打视频电话","3聊米/分钟","去完成",false);
        TaskItemBean taskItemBean7 = new TaskItemBean("打语音电话","2聊米/分钟","去完成",false);
        TaskItemBean taskItemBean8 = new TaskItemBean("发私信","1聊米/分钟","去完成",false);
        TaskItemBean taskItemBean10 = new TaskItemBean("点赞","1聊米","去完成",false);

        taskItemBeans.add(taskItemBean);
        taskItemBeans.add(taskItemBean0);
        taskItemBeans.add(taskItemBean1);
        taskItemBeans.add(taskItemBean2);
        taskItemBeans.add(taskItemBean3);
        taskItemBeans.add(taskItemBean4);
        taskItemBeans.add(taskItemBean5);
        taskItemBeans.add(taskItemBean6);
        taskItemBeans.add(taskItemBean7);
        taskItemBeans.add(taskItemBean8);
        taskItemBeans.add(taskItemBean10);
        mTaskAdapter.setList(taskItemBeans);
        mTaskRecyclerView.setAdapter(mTaskAdapter);
        TaskHttpUtil.getRewardVideoCount(new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String[] info) {
                if(info == null || info.length == 0){
                    return;
                }
                try {
                    if(code == 0){
                        int count = Integer.parseInt(info[0]);
                        if(count > 0){
                            return;
                        }
                    }
                    hasLookRewardVideoCount = false;
                }catch (Exception e){
                }
            }

            @Override
            public void onError() {
                super.onError();
                hideDialog();
            }
        });
    }

    @Override
    public void onItemClick(TaskItemBean bean, int position) {

    }

    @Override
    public void onItemChildClick(TaskItemBean bean, int position) {
        if(bean.getName().equals("任务赚")){
            TaskCenterActivity.forward(mContext);
        }else if(bean.getName().equals("激励视频任务")){
            openTaskRewardVideo();
        }
    }


    public void openTaskRewardVideo(){
        if(!hasLookRewardVideoCount){
            ToastUtil.show("今日观看次数已达上限");
            return;
        }
        if(System.currentTimeMillis() - startTime < 3000){
            ToastUtil.show("不能连续观看,请间隔3秒后在来");
            return;
        }
        showDialog();
        TaskHttpUtil.getRewardVideoCount(new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String[] info) {
                if(info == null || info.length == 0){
                    ToastUtil.show("接口错误");
                    return;
                }
                try {
                    if(code == 0){
                        int count = Integer.parseInt(info[0]);
                        if(count > 0){
                            lookVideo();
                        }else{
                            hideDialog();
                            hasLookRewardVideoCount = false;
                            ToastUtil.show("今日观看次数已达上限");
                        }
                    }else{
                        hideDialog();
                        hasLookRewardVideoCount = false;
                        ToastUtil.show(msg);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onError() {
                super.onError();
                hideDialog();
            }
        });
    }

    private void lookVideo() {
        AdSdk.getInstance().loadRewardVideoAd(this, "rv1", false,
                new AdSdk.RewardVideoAdListener() {
                    @Override
                    public void onAdLoad(String id) {
                        Log.d(TAG, "RewardVideoAd onAdLoad");
                    }

                    @Override
                    public void onVideoCached(String id) {
                        Log.d(TAG, "RewardVideoAd onVideoCached");
                    }

                    @Override
                    public void onAdShow(String id) {
                        hideDialog();
                        Log.d(TAG, "RewardVideoAd onAdShow");
                    }

                    /** 广告奖励有效性回调，onReward 一定在 onAdClose 之前，但不保证与 onVideoComplete 的先后顺序，
                     * 建议在 onAdClose 中判断 onReward 已回调才给用户奖励 */
                    @Override
                    public void onReward(String id) {
                        Log.d(TAG, "RewardVideoAd onReward");
                        isReward = true;
                    }

                    @Override
                    public void onAdClick(String id) {
                        Log.d(TAG, "RewardVideoAd onAdClick");
                    }

                    @Override
                    public void onVideoComplete(String id) {
                        Log.d(TAG, "RewardVideoAd onVideoComplete");
                    }

                    @Override
                    public void onAdClose(String id) {
                        Log.d(TAG, "RewardVideoAd onAdClose");
                        if(isReward){
                            startTime = System.currentTimeMillis();
                            TaskHttpUtil.AddExcitationRecord(new HttpCallback() {
                                @Override
                                public void onSuccess(int code, String msg, String[] info) {
                                    if(code == 0){
                                    }else{
                                        ToastUtil.show(msg);
                                    }
                                }

                                @Override
                                public void onError() {
                                    super.onError();
                                    hideDialog();
                                }
                            });
                            isReward = false;
                        }
                    }

                    @Override
                    public void onError(String id, int code, String message) {
                        Log.d(TAG, "RewardVideoAd onError: code=" + code + ", message=" + message);
                        hideDialog();
                        isReward = false;
                    }
                });
    }

    private void hideDialog() {
        if(loadingDialog != null){
            loadingDialog.dismiss();
        }
    }

    private void showDialog() {
        if(loadingDialog == null){
            loadingDialog = DialogUitl.loadingDialog(this);
        }
        loadingDialog.show();
    }

}
